package yl.hs.bmipfull.controllers.salemanage;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yl.hs.bmipfull.controllers.ControllerBase;
import yl.hs.bmipfull.pojo.salemanage.SaleOrderEntity;
import yl.hs.bmipfull.service.salemanage.ISaleOrderService;
import yl.hs.bmipfull.service.system.ISearchViewService;
import yl.hs.bmipfull.utils.datastate.SaleOrderState;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.PageResult;
import yl.hs.bmipfull.viewmodels.SubmitModel;
import yl.hs.bmipfull.viewmodels.WebResult;

import java.util.HashMap;
import java.util.Map;

/**
 * SaleOrderController
 *
 * @author 谢景广
 * @category
 * @date 2021-06-02
 */
@Api(value = "销售订单Web接口", tags = "销售管理——销售订单管理")
@RestController
@RequestMapping("/api/saleOrder")
public class SaleOrderController extends ControllerBase {

    @Autowired
    ISaleOrderService orderSvc;
    @Autowired
    ISearchViewService svSVC;


    @ApiOperation(value = "保存销售订单", notes = "查询/更新销售订单")
    @RequestMapping(value = "/order", method = RequestMethod.PUT)
    public WebResult saveMenu(@ApiParam(value = "提交信息") @RequestBody SubmitModel submitModel) {
        Integer result;
        try {
            result = orderSvc.saveSaleTask(submitModel.getData(), submitModel.getSn());
            if (result > 0) {
                return success("保存成功");
            }else if(result==-1){
                return fail("合同余额不足");
            }
            else if(result==0){
                return success("已创建任务无法调整");
            }else{
                return fail("数据未保存");
            }
        } catch (Exception e) {
            return error(e);
        }

    }

    @ApiOperation(value = "查询销售订单", notes = "分页查询销售订单信息")
    @RequestMapping(path = "/order/{rowNum}/{page}", method = RequestMethod.POST)
    @ResponseBody
    public WebResult queryPage(@ApiParam("分页查询参数") @RequestBody PageQuery query,
                               @ApiParam("页面显示数据数量") @PathVariable int rowNum,
                               @ApiParam("页码") @PathVariable int page) {
        try {
            PageResult<Object> result = orderSvc.queryPage(query, rowNum, page);
            svSVC.updateUserMenuView(query);
            return success("ok", result);
        } catch (Exception ee) {
            return error(ee);
        }
    }
    @ApiOperation(value = "查询", notes = "查询")
    @RequestMapping(value = {"/dfquerylist/{key}","/dfquerylist"}, method = RequestMethod.GET)
    public WebResult BfqueryList(@PathVariable(required = false) String key) {
        try {
            PageQuery page = new PageQuery();
            Map<String,Object> map = new HashMap<>();
            map.put("key",key);
            page.setQuery(map);
            return success("查询成功", orderSvc.queryList(page));
        } catch (Exception e) {
            e.printStackTrace();
            return fail("查询失败,查询条件异常!");
        }
    }
    @ApiOperation(value = "查询销售订单明细信息", notes = "根据销售订单明细信息编号查询销售订单信息")
    @RequestMapping(value = "/{sn}", method = RequestMethod.GET)
    public WebResult findBySn(@ApiParam(value = "编号") @PathVariable String sn) {
        return success("查询成功", orderSvc.findBySn(sn));
    }
    @ApiOperation(value = "删除销售订单信息", notes = "根据销售订单编号删除销售订单信息")
    @RequestMapping(path = "/order/{sn}", method = RequestMethod.DELETE)
    public WebResult delete(@ApiParam(value = "联系人编号", name = "sn", allowMultiple = true, required = true) @PathVariable("sn") String[] sn) {
        if (orderSvc.delete(sn) > 0) {
            return success("删除成功");
        } else {
            return fail("删除失败");
        }

    }
    @ApiOperation(value = "根据编号修改状态", notes = "根据编号修改状态")
    @RequestMapping(value = "/order/changeStateBySn/{sn}/{state}", method = RequestMethod.GET)
    @ResponseBody
    public WebResult changeStateBySn(@PathVariable String sn,@PathVariable short state) {
        try {
            Integer result = orderSvc.changeToState(sn.split(","), state);
            if (result > 0) {
                return success("数据已修改");
            } else if (result == -2) {
                return fail("数据修改失败！");
            }
            return fail("没有修改的任何数据");
        } catch (Exception ee) {
            return error(ee);
        }
    }
    @PostMapping("order/cancel/{sn}")
    @ApiOperation(value = "撤销销售订单信息", notes = "撤销订单信息" )
    public WebResult cancelSaleOrder(@PathVariable("sn")String sn){
        try{
            var result =orderSvc.cancelOrder(sn, SaleOrderState.Draft.code);
            if(result>0) {
                return success("撤销成功");
            }
            else{
                return fail("已生产不能撤销");
            }
        }
        catch (Exception ee){
            return error(ee);
        }
    }
}
